//
//  WmcUserSettingsDBManager.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/4/29.
//

import Foundation

// MARK: - 扩展WmcUserSettingsDBManager + WmcDBProtocol

extension WmcUserSettingsDBManager: WmcDBProtocol {
    static var tableName: String = "user_settings"

    static var dataType: [WmcDBManager.DataType] {
        return [
            .primaryKey(),
            .varchar(name: "user_id"),
            .integer(name: "auto_upload"),
            .varchar(name: "default_project_id"),
        ]
    }
}

// MARK: - [WmcUserSettingsDBManager]

struct WmcUserSettingsDBManager {
    /// <#Description#>
    /// - Parameters:
    ///   - model: <#model description#>
    ///   - resultBlock: 结果
    static func insert(model: WmcUserSettingsModel,
                       resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        let autoUpload = model.autoUpload ? 1 : 0

        let dataList: [WmcDBManager.InsertData] = [
            .init(column: Self.dataType[1], value: model.userId),
            .init(column: Self.dataType[2], value: autoUpload),
            .init(column: Self.dataType[3], value: model.defaultProjectId),
        ]

        WmcDBManager.shared.insertData(tableName: Self.tableName, insertData: dataList, resultBlock: resultBlock)
    }

    /// <#Description#>
    /// - Parameters:
    ///   - userId: <#userId description#>
    ///   - resultBlock: 结果
    static func query(userId: String,
                      resultBlock: YPBlock.OneParam<WmcUserSettingsModel?>? = nil)
    {
        guard !userId.isEmpty else {
            resultBlock?(nil)
            return
        }

        WmcDBManager.shared.searchCustomData(tableName: Self.tableName,
                                             condition: "where user_id = \"\(userId)\"") { list in
            let settingModel: WmcUserSettingsModel? = WmcUserSettingsModel.deserialize(from: list.first)
            resultBlock?(settingModel)
        }
    }

    /// <#Description#>
    /// - Parameters:
    ///   - autoUpload: <#autoUpload description#>
    ///   - defaultProjectId: <#defaultProjectId description#>
    ///   - resultBlock: 结果
    static func update(autoUpload: Bool? = nil,
                       defaultProjectId: String? = nil,
                       resultBlock: YPBlock.OneParam<Bool>? = nil)
    {
        var dataList: [WmcDBManager.InsertData] = []

        if let autoUpload = autoUpload {
            dataList.append(.init(column: Self.dataType[2], value: autoUpload))
        }

        if let defaultProjectId = defaultProjectId, !defaultProjectId.isEmpty {
            dataList.append(.init(column: Self.dataType[3], value: defaultProjectId))
        }

        guard !dataList.isEmpty,
              let userId = WmcLoginManager.shared.yupaoAccountModel?.userId else
        {
            resultBlock?(false)
            return
        }

        WmcDBManager.shared.updateData(tableName: Self.tableName,
                                       updateData: dataList,
                                       condition: "where user_id = \"\(userId)\"",
                                       resultBlock: resultBlock)
    }
}
